#!/usr/bin/env python
#-*- coding: utf-8 -*-
# -------------------------------------------
# Filename:    get_proxy_ip.py
# Revision:    1.0
# Date:        2012-4-8
# Author:      simon-zzm
# WEB:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------------------
import os
import threading
import re
import urllib2
import socket
import time

check_timeout =30
socket.setdefaulttimeout(check_timeout)

def check_proxy_status(proxy_address):
    print "go %s"%proxy_address
    proxy_handler = urllib2.ProxyHandler({"http" :'http://%s'%proxy_address})
    opener = urllib2.build_opener(proxy_handler)
    urllib2.install_opener(opener)
    header = {'Accept-Charset':'GBK,gb2312,utf-8;q=0.7,*;q=0.3',
              'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16'} 
    try:
        #print "start %s"%test_url
        request = urllib2.Request('http://%s'%test_url,headers = header)
        all_html = urllib2.urlopen(request).read()
        all_get_title=re.findall('title>(.*)</title',all_html,re.IGNORECASE)
    except:
        pass
        all_get_title = ["error"]
    #print "title %s"%all_get_title[0]
    if len(all_get_title) > 0 :
        try :
            get_title=all_get_title[0]
        except :
            get_title=all_get_title[0]
        from string import strip
        if strip(get_title) == strip(test_title):
            test_proxy.append(proxy_address)
    print "================="

def main():
    #### 定义循环序列，就是一个线程池
    threads = []
    #### 定义总共运行的次数
    get_proxy_ip = open("proxy_ip.txt","rb").readlines()
    all_number = len(get_proxy_ip)
    #### 定义运行所使用的线程数
    thread_lines = 30
    #### 定义开始线程数
    start_line = 0
    #### 首先构建线程池
    for i in range(0,thread_lines):
        t = threading.Thread(target=check_proxy_status, args=(get_proxy_ip[i],))
        threads.append(t)
        start_line +=1
    #### 运行第一批线程的任务
    for t in threads:
        t.start()
    #### 循环运行全部任务
    for number_line in xrange(start_line,all_number):
        #### 初始化当前线程的状态
        thread_status = False
        #### 初始化检查循环线程的开始值
        loop_line = 0
        #### 开始循环检查线程池中的线程状态
        while thread_status == False :
            #### 如果检查当前线程，如果线程停止，代表任务完成，则分配给此线程新任务，
            #### 如果检查当先线程正在运行，则开始检查下一个线程，直到分配完新任务。
            #### 如果线程池中线程全部在运行，则开始从头检查
            if threads[loop_line].isAlive() == False :
                t = threading.Thread(target=check_proxy_status, args=(get_proxy_ip[number_line],))
                threads[loop_line]=t
                threads[loop_line].start()
                thread_status = True
            else:
                if loop_line >= thread_lines-1 :
                    loop_line=0
                else:
                    loop_line+=1
                    
if __name__ == "__main__":
    try:
        os.remove('test_ip.txt')
    except:
        pass
    #### test result
    test_proxy = []
    ####
    test_file = open("check_url.txt","rb").readlines()
    test_re = re.match('(.*)::(.*)',test_file[0],re.DOTALL)
    test_re.group()
    test_url = test_re.group(1)
    test_title = test_re.group(2) 
    main()
    time.sleep(check_timeout*5)
    #print test_proxy
    file = open("test_ip.txt", "ab")
    for get in test_proxy:
        file.write("%s" % get)
    file.close()
    print "ok"
